#include <bits/stdc++.h>
// 2025/01/30
// tag: 
// Author: Zhang Muen
using namespace std;

int n, cnt[1000001];
vector<pair<int, int>> graph[1000001];
bool vis[1000001];

int dfs(int x){
    int64_t res = 0;
    for(auto &p: graph[x]){
        int64_t v = p.first;
        if(!vis[v])
            vis[v] = true, res += dfs(v);
    }
    return (cnt[x] = res + 1);
}

signed main()
{
    cin >> n;
    for (int i = 0; i < n - 1; i++){
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
    }
    vis[1] = true, dfs(1);
    int64_t ans = 0;
    for (int i = 1; i <= n; i++)
        for (auto x: graph[i])
            if (cnt[x.first] > cnt[i])
                ans += 1LL * x.second * abs(n - cnt[i] * 2);
    cout << ans << endl;
    return 0;
}